<?php
/**
 * Created by PhpStorm.
 * User: cgx
 * Date: 2016/8/11
 * Time: 16:22
 */
namespace spider\Controller;
use Think\Controller;
use QL\QueryList;
use Sunra\PhpSimple\HtmlDomParser;
set_time_limit(0);
error_reporting(0);

class ZhanqiController extends Controller{

    public function index(){
        $start_time = time();

        $game_type = C('ZHANQI_GAME');
        $page = 1;
        $top_views = 100;

        $is_update = true;
        while ($page<5){
            $zhanqi_api = sprintf(C('ZHANQI_API'),$page);
            echo $zhanqi_api;

            $response = QueryList::Query($zhanqi_api,array('html'=>'html'))->html;
            if($response && strlen($response) != 0){
                $res_arr = json_decode($response,true);
                $res_list = $res_arr['data']['rooms'];

                foreach ($res_list as $k => $v){
                    $game_host_name = $v['gameName'];
                    if(!array_key_exists($game_host_name,$game_type)){
                        continue;
                    }
                    $views = intval($v['online']);
                    if($views < $top_views){
                        continue;
                    }
                    $kw['title'] = $v['title'];
                    $kw['thumb'] = $v['bpic'];
                    $kw['keywords'] ='';
                    $kw['description'] = '';
                    $kw['inputtime'] = time();
                    $kw['updatetime'] = time();
                    $kw['zb_thumb'] = $v['avatar'].'-medium';
                    $kw['zbid'] = $v['id'];
                    $kw['zbname'] = $v['nickname'];
                    $kw['source'] = 'zhanqi';
                    $kw['category'] = $game_type[$game_host_name];
                    $kw['isOnlive'] = '1';
                    $kw['livedata'] = 'http://www.zhanqi.tv/live/embed?roomId='.$v['id'];
                    $kw['m_livedata'] = '';
                    $kw['views'] = $views;
                    if($is_update){
                        befor_update('huya');
                        update_zhibo('huya',$kw);
                        $is_update=false;
                    }else{
                        update_zhibo('huya',$kw);
                    }
                }
            }
            $page+=1;
        }

        echo 'time cost '.(time()-$start_time).' seconds';
    }


}